const { equal, notEqual } = require('assert');
const configInfo = require('./config.json');

const configKeys = ['cfg', 'port'];

class Config {
	constructor() {
		this.config = {
			port: 5555,
			'watch-path': './',
			'path-blacklist': [],
			'suffix-blacklist': []
		}
		if (configInfo) {
			notEqual(Array.isArray(configInfo), true, `config must be of type object! cannot be a [Array]!`)
			equal(typeof configInfo, 'object', `config must be of type object! cannot be a [${typeof configInfo}]!`)
			verifyConfigInfo(configInfo)
			configInfo['suffix-blacklist'] = configInfo['suffix-blacklist'].map(item => item.toLocaleLowerCase())
			this.config = Object.assign(this.config, configInfo);
		}
		this.config = Object.assign(this.config, processArgvToConfig())
		console.log(this.config)
	}

	getValue (key) {
		equal(typeof key, 'string', 'key must be of type string!')
		notEqual(this.config[key], undefined, 'You are trying to get a data that does not exist!')
		return this.config[key]
	}
}


const verifyConfigInfo = () => {

}

/**
 * 处理启动时 argv 参数到 config 中 
 */
const processArgvToConfig = () => {
	const config = {
		'start-path': process.cwd()
	}
	for (const item of process.argv) {
		for (const key of configKeys) {
			if (item.indexOf(key) == 0) {
				config[key] = item.replace(`${key}=`, '')
			}
		}
	}
	return config
}

module.exports = Config;
